package com.grandtech.insurance.common.bean;

import com.alibaba.fastjson.JSON;
import com.grandtech.insurance.common.utils.trans.SimpleMap;
import com.mapbox.geojson.Feature;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CarriesFeature extends Carries {

    SimpleMap<String, Object> carries;

    /**
     * ****************************************简单操作 key:表名，val:feature********************************
     * ****************************************简单操作 key:表名，val:feature********************************
     */
    public CarriesFeature addFeature(String tableName, Feature feature) {
        if (tableName == null || feature == null) return this;
        if (carries == null) carries = new SimpleMap<>();
        carries.push(tableName, feature.toJson());
        return this;
    }

    public SimpleMap<String, Feature> obtainFeatures() {
        if (carries == null || carries.size() == 0) {
            return null;
        }
        SimpleMap<String, Feature> _carries = new SimpleMap<>();
        for (String key : carries.keySet()) {
            String featureJson = carries.get(key).toString();
            _carries.push(key, Feature.fromJson(featureJson));
        }
        return _carries;
    }

    public SimpleMap<String, Feature> obtainFeaturesV1() {
        if (carries == null || carries.size() == 0) return null;
        SimpleMap<String, Feature> _carries = new SimpleMap<>();
        for (String key : carries.keySet()) {
            String featureJson = JSON.toJSONString(carries.get(key));
            _carries.push(key, Feature.fromJson(featureJson));
        }
        return _carries;
    }
//    public SimpleMap<String, Feature> obtainFeatures() {
//        if (carries == null || carries.size() == 0) return null;
//        SimpleMap<String, Feature> _carries = new SimpleMap<>();
//        for (String key : carries.keySet()) {
//            String featureJson = carries.get(key).toString();
//            _carries.push(key, Feature.fromJson(featureJson));
//        }
//        return _carries;
//    }


    public Feature obtainFeatureDefault() {
        SimpleMap<String, Feature> obtainFeatures = obtainFeatures();
        if (obtainFeatures == null || obtainFeatures.size() == 0) return null;
        for (String key : obtainFeatures.keySet()) {
            return obtainFeatures.get(key);
        }
        return null;
    }

    public CarriesFeature addFeatures(String tableName, List<Feature> feature) {
        if (tableName == null || feature == null) return this;
        if (carries == null) carries = new SimpleMap<>();
        List<String> featureJsons = new ArrayList<>();
        for (int i = 0; i < feature.size(); i++) {
            featureJsons.add(feature.get(i).toJson());
        }
        carries.push(tableName, featureJsons);
        return this;
    }

    public List<Feature> obtainFeatures(String tableName) {
        if (tableName == null || carries == null) return null;
        List<String> featureJsons = (List<String>) carries.get(tableName);
        List<Feature> features = new ArrayList<>();
        for (int i = 0; i < featureJsons.size(); i++) {
            features.add(Feature.fromJson(featureJsons.get(i)));
        }
        return features;
    }

    public Map<String, List<Feature>> obtainFeatureMapList() {
        if (carries == null) return null;
        Map<String, List<Feature>> res = new SimpleMap<>();
        for (String tableName : carries.keySet()) {
            List<String> featureJsons = (List<String>) carries.get(tableName);
            List<Feature> features = new ArrayList<>();
            for (int i = 0; i < featureJsons.size(); i++) {
                features.add(Feature.fromJson(featureJsons.get(i)));
            }
            res.put(tableName, features);
        }
        return res;
    }

    /**
     * ****************************************简单操作 key:表名，val:feature********************************
     * ****************************************简单操作 key:表名，val:feature********************************
     */
    public Map<String, Object> obtainFeatureDefaultProperties() {
        SimpleMap<String, Object> carries = getCarries();
        for (String key : carries.keySet()) {
            String featureStr = carries.get(key).toString();
            Feature feature = Feature.fromJson(featureStr);
            return JSON.parseObject(feature.properties().toString(), Map.class);
        }
        return null;
    }


    public SimpleMap<String, Object> getCarries() {
        return carries;
    }

    public void setCarries(SimpleMap<String, Object> carries) {
        this.carries = carries;
    }

    public static CarriesFeature create() {
        return new CarriesFeature();
    }
}
